package com.office.automate.service.impl.employee;

import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.office.automate.configuration.property.ConfigurationProperties;
import com.office.automate.dataaccess.def.user.EmployeeDao;
import com.office.automate.dataaccess.impl.user.EmployeeDaoImplementer;
import com.office.automate.email.framework.HandlingEmails;
import com.office.automate.employee.data.validator.EmployeeDataValidator;
import com.office.automate.encrypt.password.EncryptPassword;
import com.office.automate.exception.framework.FrontierVisibleException;
import com.office.automate.frontier.utility.DataConversionUtility;
import com.office.automate.model.employee.Employee;
import com.office.automate.model.employee.EmployeeAttandanceCommit;
import com.office.automate.model.employee.EmployeeAttandanceQueryObject;
import com.office.automate.model.employee.EmployeeAttendance;
import com.office.automate.model.employee.EmployeeOverTime;
import com.office.automate.model.employee.EmployeeQualification;
import com.office.automate.model.employee.EmployeeSearch;
import com.office.automate.service.def.employee.EmployeeService;
import com.office.automate.statics.ApplicationStatics;

public class EmployeeServiceImplementer implements EmployeeService {

	private EmployeeDao employeeDao = new EmployeeDaoImplementer();
	private DataConversionUtility dcu = new DataConversionUtility();
	private EmployeeDataValidator employeeDataValidator = new EmployeeDataValidator();
	private HandlingEmails handlingEmails = new HandlingEmails();
	private Properties properties = ConfigurationProperties.getProps();
	private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

	public HashMap<String, FrontierVisibleException> validateEmployeeDetails(Employee anEmployee) throws FrontierVisibleException {
		HashMap<String, FrontierVisibleException> fieldValidationErrors = null;

		employeeDataValidator.validateNewEmployeeRegistrationDetails(anEmployee);

		if (employeeDataValidator.isValidationErrorsExist()) {
			fieldValidationErrors = employeeDataValidator.getFieldValidationErrors();
		}
		return fieldValidationErrors;
	}

	public boolean validateUniquenessOfEmailAddress(String aEmailAddress) {
		return false;
	}

	public boolean checkAvailabilityOfUsername(String aUsername) {
		return false;
	}

	public Employee registerNewEmployee(Employee anEmployee) throws FrontierVisibleException {

		String unEncryptPassword = anEmployee.getPassword();
		anEmployee.setPassword(new EncryptPassword().getEncryptedPasswordMD5(anEmployee.getPassword()));
		employeeDao.getNextEmployeeNumber(anEmployee);
		employeeDao.persistEmployeeDetails(anEmployee);

		
		// Send confirmation email to registered user.
		handlingEmails.sendEmailTo(
						anEmployee.getEmail(),
						properties.getProperty("MAIL_REGISTER_ACCOUNT_MAIL_SUBJECT"),
						getEmailBody(anEmployee, unEncryptPassword));
		return anEmployee;
	}

	private String getEmailBody(Employee anEmployee, String anUnEncryptPassword) {
		String emailBodyText = "";
		String emailBodyTemplate = properties.getProperty("MAIL_REGISTER_ACCOUNT_MAIL_BODY");
		emailBodyText = MessageFormat.format(emailBodyTemplate,
											 anEmployee.getFirstName() + " " + anEmployee.getLastName(),
											 anEmployee.getUsername(),
											 anUnEncryptPassword,
											 anEmployee.getEmployeeId());
		return emailBodyText;
	}

	public HashMap<String, FrontierVisibleException> validateEmployeeQualificationDetails(EmployeeQualification anEmployeeQualification) throws FrontierVisibleException {
		HashMap<String, FrontierVisibleException> fieldValidationErrors = null;

		employeeDataValidator.validateEmployeeQualificationDetails(anEmployeeQualification);

		if (employeeDataValidator.isValidationErrorsExist()) {
			fieldValidationErrors = employeeDataValidator.getFieldValidationErrors();
		}
		return fieldValidationErrors;
	}

	public EmployeeQualification addEmployeeQualification(EmployeeQualification anEmployeeQualification) throws FrontierVisibleException {
		employeeDao.persistEmployeeQualificationDetails(anEmployeeQualification);

		return anEmployeeQualification;
	}

	public void deleteQualification(EmployeeQualification employeeQualification) throws FrontierVisibleException {
		employeeDao.deleteQualification(employeeQualification);
		
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<List> getEmployeeQualificationPage(int aStartingIndex, int aNumberOfRecords) throws FrontierVisibleException {
		List<List> anEmployeeQualificationList = new ArrayList<List>();

		List<EmployeeQualification> employeeQualifications = employeeDao.getEmployeeQualificationPage(aStartingIndex, aNumberOfRecords);
	
		for (EmployeeQualification employeeQualification :employeeQualifications) {
			List anEmployeeQualificationDataRow = new ArrayList();
			anEmployeeQualificationDataRow.add(employeeQualification.getEmployeeId());
			anEmployeeQualificationDataRow.add(properties.getProperty("QUALIFICATION_" + employeeQualification.getType()));
			anEmployeeQualificationDataRow.add(employeeQualification.getDescription());
			anEmployeeQualificationDataRow.add(formatter.format(employeeQualification.getStartDate()));
			anEmployeeQualificationDataRow.add(formatter.format(employeeQualification.getEndDate()));
			anEmployeeQualificationDataRow.add(employeeQualification.getDataRowId());
			anEmployeeQualificationList.add(anEmployeeQualificationDataRow);
		}
		return anEmployeeQualificationList;
	}

	public int getAvailableEmployeeQualificationDetailRecordCount() throws FrontierVisibleException {
		
		return employeeDao.getAvailableEmployeeQualificationDetailRecordCount();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<List> getSearchResultPage(EmployeeSearch anEmployeeSearch, int aStartingIndex, int aNumberOfRecords) throws FrontierVisibleException {
		List<List> anEmployeeSearchList = new ArrayList<List>();

		List<Employee> employeeSearch = employeeDao.getSearchResultPage(anEmployeeSearch, aStartingIndex, aNumberOfRecords);

		for (Employee employee :employeeSearch) {
			List anEmployeeDataRow = new ArrayList();
			anEmployeeDataRow.add("");
			anEmployeeDataRow.add(employee.getEmployeeId());
			anEmployeeDataRow.add(employee.getFirstName());
			anEmployeeDataRow.add(employee.getLastName());
			anEmployeeDataRow.add(employee.getContactNo());
			anEmployeeSearchList.add(anEmployeeDataRow);
		}
		return anEmployeeSearchList;
	}

	public int getAvailableEmployeeDetailRecordCount(EmployeeSearch aSearchCriteria) throws FrontierVisibleException {
		return employeeDao.getAvailableEmployeeDetailRecordCount(aSearchCriteria);
	}

	public void deleteEmployee(Employee employee) throws FrontierVisibleException {
		employeeDao.deleteEmployee(employee);
	}

	public Employee getEmployeeDetails(long anEmployeeId) throws FrontierVisibleException {
		return employeeDao.getEmployeeDetails(anEmployeeId);
	}

	public void updateEmployee(Employee anUpdateEmployee) throws FrontierVisibleException {
		employeeDao.updateEmployee(anUpdateEmployee);
	}

	public HashMap<String, FrontierVisibleException> validateUpdateEmployeeDetails(Employee anEmployee) throws FrontierVisibleException {
		HashMap<String, FrontierVisibleException> fieldValidationErrors = null;

		employeeDataValidator.validateUpdateEmployeeRegistrationDetails(anEmployee);

		if (employeeDataValidator.isValidationErrorsExist()) {
			fieldValidationErrors = employeeDataValidator.getFieldValidationErrors();
		}
		return fieldValidationErrors;
	}

	public List<EmployeeQualification> getEmployeeQualificationDetails(long anEmployeeId) throws FrontierVisibleException {

		return employeeDao.getEmployeeQualificationDetails(anEmployeeId);
	}

	public void uploadProfileImage(Employee updateEmployee) throws FrontierVisibleException {
		employeeDao.uploadProfileImage(updateEmployee);
	}

	public EmployeeAttendance getEmployeeAttendanceDetailsForCurrentWeek(long employeeId) throws FrontierVisibleException {
	
		Calendar tdt = Calendar.getInstance();
		Calendar weekDay = Calendar.getInstance();
		EmployeeAttendance employeeAttendance = new EmployeeAttendance();
		EmployeeAttandanceQueryObject attandanceQuaryObject = new EmployeeAttandanceQueryObject();
		attandanceQuaryObject.setEmployeeId(employeeId);
		attandanceQuaryObject.setStartDateOfWeek(formatter.format(dcu.getDatesOfGivenWeek(tdt.getTime(), 1)));
		attandanceQuaryObject.setEndDateOfWeek(formatter.format(dcu.getDatesOfGivenWeek(tdt.getTime(), 7)));
		HashMap<String, Date> attendanceSwappings = new HashMap<String, Date>();

		List<EmployeeAttandanceCommit> employeeAttandanceCommitList = employeeDao.getAttandanceDetails(attandanceQuaryObject);
		for (EmployeeAttandanceCommit empAttComt : employeeAttandanceCommitList) {
			weekDay.setTime(empAttComt.getAttandanceDate());
			int dayOfWeek = weekDay.get(Calendar.DAY_OF_WEEK);
			//boolean 
			if (dayOfWeek == Calendar.SUNDAY) {
				if (isAttendanceLevelIn(empAttComt.getSwappingLevel())) {
					employeeAttendance.setSundayIn(true);
					attendanceSwappings.put("SUN-IN", empAttComt.getSwappingDateTime());
				} else if (isAttendanceLevelOut(empAttComt.getSwappingLevel())) {
					employeeAttendance.setSundayOut(true);
					attendanceSwappings.put("SUN-OUT", empAttComt.getSwappingDateTime());
				}
			} else if (dayOfWeek == Calendar.MONDAY) {
				if (isAttendanceLevelIn(empAttComt.getSwappingLevel())) {
					employeeAttendance.setMondayIn(true);
					attendanceSwappings.put("MON-IN", empAttComt.getSwappingDateTime());
				} else if (isAttendanceLevelOut(empAttComt.getSwappingLevel())) {
					employeeAttendance.setMondayOut(true);
					attendanceSwappings.put("MON-OUT", empAttComt.getSwappingDateTime());
				}
			} else if (dayOfWeek == Calendar.TUESDAY) {
				if (isAttendanceLevelIn(empAttComt.getSwappingLevel())) {
					employeeAttendance.setTuesdayIn(true);
					attendanceSwappings.put("TUE-IN", empAttComt.getSwappingDateTime());
				} else if (isAttendanceLevelOut(empAttComt.getSwappingLevel())) {
					employeeAttendance.setTuesdayOut(true);
					attendanceSwappings.put("TUE-OUT", empAttComt.getSwappingDateTime());
				}
			} else if (dayOfWeek == Calendar.WEDNESDAY) {
				if (isAttendanceLevelIn(empAttComt.getSwappingLevel())) {
					employeeAttendance.setWednesdayIn(true);
					attendanceSwappings.put("WED-IN", empAttComt.getSwappingDateTime());
				} else if (isAttendanceLevelOut(empAttComt.getSwappingLevel())) {
					employeeAttendance.setWednesdayOut(true);
					attendanceSwappings.put("WED-OUT", empAttComt.getSwappingDateTime());
				}
			} else if (dayOfWeek == Calendar.THURSDAY) {
				if (isAttendanceLevelIn(empAttComt.getSwappingLevel())) {
					employeeAttendance.setThursdayIn(true);
					attendanceSwappings.put("THU-IN", empAttComt.getSwappingDateTime());
				} else if (isAttendanceLevelOut(empAttComt.getSwappingLevel())) {
					employeeAttendance.setThursdayOut(true);
					attendanceSwappings.put("THU-OUT", empAttComt.getSwappingDateTime());
				}
			} else if (dayOfWeek == Calendar.FRIDAY) {
				if (isAttendanceLevelIn(empAttComt.getSwappingLevel())) {
					employeeAttendance.setFridayIn(true);
					attendanceSwappings.put("FRI-IN", empAttComt.getSwappingDateTime());
				} else if (isAttendanceLevelOut(empAttComt.getSwappingLevel())) {
					employeeAttendance.setFridayOut(true);
					attendanceSwappings.put("FRI-OUT", empAttComt.getSwappingDateTime());
				}
			} else if (dayOfWeek == Calendar.SATURDAY) {
				if (isAttendanceLevelIn(empAttComt.getSwappingLevel())) {
					employeeAttendance.setSaturdayIn(true);
					attendanceSwappings.put("SAT-IN", empAttComt.getSwappingDateTime());
				} else if (isAttendanceLevelOut(empAttComt.getSwappingLevel())) {
					employeeAttendance.setSaturdayOut(true);
					attendanceSwappings.put("SAT-OUT", empAttComt.getSwappingDateTime());
				}
			}
		}
		// Calcualte working hours
		if (attendanceSwappings.containsKey("SUN-IN")) {
			double workingHours = 0.0;
			if (attendanceSwappings.containsKey("SUN-OUT")) {
				workingHours = getWorkingHours(attendanceSwappings.get("SUN-IN"), attendanceSwappings.get("SUN-OUT"));
			} else {
				workingHours = getWorkingHours(attendanceSwappings.get("SUN-IN"), new Date());
			}
			employeeAttendance.setSundayWorkingHours(workingHours);
		} 
		if (attendanceSwappings.containsKey("MON-IN")) {
				double workingHours = 0.0;
				if (attendanceSwappings.containsKey("MON-OUT")) {
					workingHours = getWorkingHours(attendanceSwappings.get("MON-IN"), attendanceSwappings.get("MON-OUT"));
				} else {
					workingHours = getWorkingHours(attendanceSwappings.get("MON-IN"), new Date());
				}
				employeeAttendance.setMondayWorkingHours(workingHours);
		}
		if (attendanceSwappings.containsKey("TUE-IN")) {
			double workingHours = 0.0;
			if (attendanceSwappings.containsKey("TUE-OUT")) {
				workingHours = getWorkingHours(attendanceSwappings.get("TUE-IN"), attendanceSwappings.get("TUE-OUT"));
			} else {
				workingHours = getWorkingHours(attendanceSwappings.get("TUE-IN"), new Date());
			}
			employeeAttendance.setTuesdayWorkingHours(workingHours);
		} 
		if (attendanceSwappings.containsKey("WED-IN")) {
			double workingHours = 0.0;
			if (attendanceSwappings.containsKey("WED-OUT")) {
				workingHours = getWorkingHours(attendanceSwappings.get("WED-IN"), attendanceSwappings.get("WED-OUT"));
			} else {
				workingHours = getWorkingHours(attendanceSwappings.get("WED-IN"), new Date());
			}
			employeeAttendance.setWednesdayWorkingHours(workingHours);
		}
		if (attendanceSwappings.containsKey("THU-IN")) {
			double workingHours = 0.0;
			if (attendanceSwappings.containsKey("THU-OUT")) {
				workingHours = getWorkingHours(attendanceSwappings.get("THU-IN"), attendanceSwappings.get("THU-OUT"));
			} else {
				workingHours = getWorkingHours(attendanceSwappings.get("THU-IN"), new Date());
			}
			employeeAttendance.setTuesdayWorkingHours(workingHours);
		}
		if (attendanceSwappings.containsKey("FRI-IN")) {
			double workingHours = 0.0;
			if (attendanceSwappings.containsKey("FRI-OUT")) {
				workingHours = getWorkingHours(attendanceSwappings.get("FRI-IN"), attendanceSwappings.get("FRI-OUT"));
			} else {
				workingHours = getWorkingHours(attendanceSwappings.get("FRI-IN"), new Date());
			}
			employeeAttendance.setFridayWorkingHours(workingHours);
		}
		if (attendanceSwappings.containsKey("SAT-IN")) {
			double workingHours = 0.0;
			if (attendanceSwappings.containsKey("SAT-OUT")) {
				workingHours = getWorkingHours(attendanceSwappings.get("SAT-IN"), attendanceSwappings.get("SAT-OUT"));
			} else {
				workingHours = getWorkingHours(attendanceSwappings.get("SAT-IN"), new Date());
			}
			employeeAttendance.setSaturdayWorkingHours(workingHours);
		}

		return employeeAttendance;
	}

	private double getWorkingHours(Date arrivalTime, Date departureTime) {
		return (departureTime.getTime() - arrivalTime.getTime()) / (double)(60*60*1000);
	}

	public HashMap<String, FrontierVisibleException> validateOTRequestDetails(EmployeeOverTime employeeOverTime) throws FrontierVisibleException {
		HashMap<String, FrontierVisibleException> fieldValidationErrors = null;

		employeeDataValidator.validateEmployeeOTRequestDetails(employeeOverTime);

		if (employeeDataValidator.isValidationErrorsExist()) {
			fieldValidationErrors = employeeDataValidator.getFieldValidationErrors();
		}
		return fieldValidationErrors;
	}

	public void addEmployeeOTRequest(EmployeeOverTime employeeOverTime) throws FrontierVisibleException {
		Map<String, Object> leaveReqiuestDetails = employeeDao.addEmployeeOTRequest(employeeOverTime);
		Employee OTRequester = (Employee)leaveReqiuestDetails.get("OTRequesterDetails");
		Employee OTApprover = (Employee)leaveReqiuestDetails.get("OTApproverDetails");

		// Send confirmation email to leave approver.
		handlingEmails.sendEmailTo(
						OTApprover.getEmail(),
						MessageFormat.format(properties.getProperty("MAIL_OT_APPROVER_MAIL_SUBJECT"),
											 OTRequester.getFirstName() + " " + OTRequester.getLastName()),
						getEmailBodyForApprover(ApplicationStatics.OT_REQUEST_APPROVE_MAIL, OTApprover, employeeOverTime));

		// Send confirmation email to leave requester.
		handlingEmails.sendEmailTo(
						OTRequester.getEmail(),
						properties.getProperty("MAIL_OT_REQUESTER_MAIL_SUBJECT"),
						getEmailBodyForRequester(ApplicationStatics.OT_REQUEST_APPROVE_MAIL, OTRequester, OTApprover, employeeOverTime));
		
	}

	public void cancelOverTimeRequest(long aRequestId) throws FrontierVisibleException {
		Map<String, Object> OTReqiuestDetails = employeeDao.cancelOTRequest(aRequestId);

		Employee OTRequester = (Employee)OTReqiuestDetails.get("OTRequesterDetails");
		Employee OTApprover = (Employee)OTReqiuestDetails.get("OTApproverDetails");
		EmployeeOverTime employeeOverTime = (EmployeeOverTime)OTReqiuestDetails.get("EmployeeOverTime");

		// Send confirmation email to lieu leave approver.
		handlingEmails.sendEmailTo(
						OTApprover.getEmail(),
						MessageFormat.format(properties.getProperty("MAIL_OT_CANCEL_APPROVER_MAIL_SUBJECT"),
										     OTRequester.getFirstName() + " " + OTRequester.getLastName()),
						getEmailBodyForApprover(ApplicationStatics.CANCEL_MAIL, OTApprover, employeeOverTime));

		// Send confirmation email to lieu leave requester.
		handlingEmails.sendEmailTo(
						OTRequester.getEmail(),
						properties.getProperty("MAIL_OT_CANCEL_REQUESTER_MAIL_SUBJECT"),
						getEmailBodyForRequester(ApplicationStatics.CANCEL_MAIL, OTRequester, OTApprover, employeeOverTime));
		
	}

	public void approveOverTimeRequest(long aRequestId) throws FrontierVisibleException {
		Map<String, Object> OTReqiuestDetails = employeeDao.approveOTRequest(aRequestId);
		Employee OTRequester = (Employee)OTReqiuestDetails.get("OTRequesterDetails");
		Employee OTApprover = (Employee)OTReqiuestDetails.get("OTApproverDetails");
		EmployeeOverTime employeeOverTime = (EmployeeOverTime)OTReqiuestDetails.get("EmployeeOverTime");

		// Send confirmation email to leave approver.
		handlingEmails.sendEmailTo(
						OTApprover.getEmail(),
						MessageFormat.format(properties.getProperty("MAIL_OT_APPROVE_APPROVER_MAIL_SUBJECT"),
										     OTRequester.getFirstName() + " " + OTRequester.getLastName()),
						getEmailBodyForApprover(ApplicationStatics.APPROVED_MAIL, OTApprover, employeeOverTime));

		// Send confirmation email to leave requester.
		handlingEmails.sendEmailTo(
						OTRequester.getEmail(),
						properties.getProperty("MAIL_OT_APPROVE_REQUESTER_MAIL_SUBJECT"),
						getEmailBodyForRequester(ApplicationStatics.APPROVED_MAIL, OTRequester, OTApprover, employeeOverTime));
	}

	public void rejectOverTimeRequest(long aRequestId) throws FrontierVisibleException {
		Map<String, Object> OTReqiuestDetails = employeeDao.rejectOTRequest(aRequestId);
		Employee OTRequester = (Employee)OTReqiuestDetails.get("OTRequesterDetails");
		Employee OTApprover = (Employee)OTReqiuestDetails.get("OTApproverDetails");
		EmployeeOverTime employeeOverTime = (EmployeeOverTime)OTReqiuestDetails.get("EmployeeOverTime");

		// Send confirmation email to leave approver.
		handlingEmails.sendEmailTo(
						OTApprover.getEmail(),
						MessageFormat.format(properties.getProperty("MAIL_OT_REJECT_APPROVER_MAIL_SUBJECT"),
										     OTRequester.getFirstName() + " " + OTRequester.getLastName()),
						getEmailBodyForApprover(ApplicationStatics.REJECT_MAIL, OTApprover, employeeOverTime));

		// Send confirmation email to leave requester.
		handlingEmails.sendEmailTo(
						OTRequester.getEmail(),
						properties.getProperty("MAIL_OT_REJECT_REQUESTER_MAIL_SUBJECT"),
						getEmailBodyForRequester(ApplicationStatics.REJECT_MAIL, OTRequester, OTApprover, employeeOverTime));
	}
	
	private String getEmailBodyForApprover(String aMailRequestType, Employee aLeaveApprover, EmployeeOverTime employeeOverTime) {
		String emailBodyText = "";
		String emailBodyTemplate = "";
		if (aMailRequestType.equals(ApplicationStatics.OT_REQUEST_APPROVE_MAIL)){
			emailBodyTemplate = properties.getProperty("MAIL_OT_APPROVER_MAIL_BODY");			
		} else if (aMailRequestType.equals(ApplicationStatics.OT_REQUEST_CANCEL_MAIL)) {
			emailBodyTemplate = properties.getProperty("MAIL_OT_CANCEL_APPROVER_MAIL_BODY");
		} else if (aMailRequestType.equals(ApplicationStatics.OT_REQUEST_APPROVED_MAIL)) {
			emailBodyTemplate = properties.getProperty("MAIL_OT_APPROVE_APPROVER_MAIL_BODY");
		} else if (aMailRequestType.equals(ApplicationStatics.OT_REQUEST_REJECT_MAIL)) {
			emailBodyTemplate = properties.getProperty("MAIL_OT_REJECT_APPROVER_MAIL_BODY");
		}

		emailBodyText = MessageFormat.format(emailBodyTemplate,
											 aLeaveApprover.getFirstName() + " " + aLeaveApprover.getLastName(),
											 formatter.format(employeeOverTime.getOvertimeDate()),
											 employeeOverTime.getNumberOfHours(),
											 employeeOverTime.getReasonForOverTime());
		return emailBodyText;
	}

	private String getEmailBodyForRequester(String aMailRequestType, Employee aLeaveRequester, Employee aLeaveApprover, EmployeeOverTime employeeOverTime) {
		String emailBodyText = "";
		String emailBodyTemplate = "";
		if (aMailRequestType.equals(ApplicationStatics.OT_REQUEST_APPROVE_MAIL)){
			emailBodyTemplate = properties.getProperty("MAIL_OT_REQUESTER_MAIL_BODY");			
		} else if (aMailRequestType.equals(ApplicationStatics.OT_REQUEST_CANCEL_MAIL)) {
			emailBodyTemplate = properties.getProperty("MAIL_OT_CANCEL_REQUESTER_MAIL_BODY");
		} else if (aMailRequestType.equals(ApplicationStatics.OT_REQUEST_APPROVED_MAIL)) {
			emailBodyTemplate = properties.getProperty("MAIL_OT_APPROVE_REQUESTER_MAIL_BODY");
		} else if (aMailRequestType.equals(ApplicationStatics.OT_REQUEST_REJECT_MAIL)) {
			emailBodyTemplate = properties.getProperty("MAIL_OT_REJECT_REQUESTER_MAIL_BODY");
		}
		emailBodyText = MessageFormat.format(emailBodyTemplate,
											 formatter.format(employeeOverTime.getOvertimeDate()),
											 employeeOverTime.getNumberOfHours(),
											 aLeaveApprover.getFirstName() + " " + aLeaveApprover.getLastName());
		return emailBodyText;
	}

	public int getAvailableEmployeeOverTimeRequestsRecordCount(Map<String, Object> aQueryCriterias) throws FrontierVisibleException {
		
		return employeeDao.getAvailableEmployeeOverTimeRequestsRecordCount(aQueryCriterias);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<List> getEmployeeOverTimeRequestPage(Map<String, Object> aQueryCriterias, int aStartingIndex, int aNumberOfRecords) throws FrontierVisibleException {
		List<List> anEmployeeOverTimeRequestsList = new ArrayList<List>();

		List<EmployeeOverTime> employeeOverTimeList = employeeDao.getEmployeeOverTimeRequestsList(aQueryCriterias, aStartingIndex, aNumberOfRecords);
	
		for (EmployeeOverTime employeeOverTime :employeeOverTimeList) {

			List anEmployeeOverTimeRequestDataRow = new ArrayList();
			anEmployeeOverTimeRequestDataRow.add(formatter.format(employeeOverTime.getOvertimeDate()));
			anEmployeeOverTimeRequestDataRow.add(employeeOverTime.getNumberOfHours());
			anEmployeeOverTimeRequestDataRow.add(new EmployeeDaoImplementer().getEmployeeName(employeeOverTime.getApproverEmployeeId()));
			anEmployeeOverTimeRequestDataRow.add(properties.getProperty(employeeOverTime.getStatus()));
			anEmployeeOverTimeRequestDataRow.add(employeeOverTime.getReasonForOverTime());
			anEmployeeOverTimeRequestDataRow.add(employeeOverTime.getRequestId());
			anEmployeeOverTimeRequestsList.add(anEmployeeOverTimeRequestDataRow);
		}
		return anEmployeeOverTimeRequestsList;

	}

	public int getAvailableEmployeeOverTimeApprovalRequestRecordCount(long aCurrentLoginUserId) throws FrontierVisibleException {

		return employeeDao.getAvailableEmployeeOverTimeApprovalRequestRecordCount(aCurrentLoginUserId);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<List> getEmployeeOverTimeApprovalRequestPage(int aStartingIndex, int aNumberOfRecords, long aCurrentLoginUserId) throws FrontierVisibleException {

		List<List> anEmployeeOverTimeRequestList = new ArrayList<List>();

		List<EmployeeOverTime> employeeOverTimeList = employeeDao.getEmployeeOverTimeApprovalRequestsList(aCurrentLoginUserId, aStartingIndex, aNumberOfRecords);
	
		for (EmployeeOverTime employeeOverTime :employeeOverTimeList) {
			List anEmployeeOverTimeDataRow = new ArrayList();
			anEmployeeOverTimeDataRow.add(formatter.format(employeeOverTime.getOvertimeDate()));
			anEmployeeOverTimeDataRow.add(employeeOverTime.getNumberOfHours());
			anEmployeeOverTimeDataRow.add(new EmployeeDaoImplementer().getEmployeeName(employeeOverTime.getEmployeeId()));
			anEmployeeOverTimeDataRow.add(employeeOverTime.getReasonForOverTime());
			anEmployeeOverTimeDataRow.add(employeeOverTime.getRequestId());
			anEmployeeOverTimeRequestList.add(anEmployeeOverTimeDataRow);
		}
		return anEmployeeOverTimeRequestList;

	}

	public void commitAttandanceDetails(EmployeeAttandanceCommit employeeAttandanceCommit) throws FrontierVisibleException {
		employeeAttandanceCommit.setSwappingDateTime(new Date());
		employeeDao.commitAttandanceDetails(employeeAttandanceCommit);
		
	}

	private boolean isAttendanceLevelIn(String value) {
		return value.indexOf("In") > 0;
	}

	private boolean isAttendanceLevelOut(String value) {
		return value.indexOf("Out") > 0;
	}

	public List<Employee> getAllEmployeeDetails() throws FrontierVisibleException {

		return employeeDao.getAllEmployeeDetail();
	}
}